using UnityEngine;
using System.Collections;

public class _Player3 : MonoBehaviour {

	float f_jump;
	
	bool jump = false;
	
	public static float f_basejump = 0;
	public static float f_coins = 20;
	public static bool b_gravity = true;	
	
	public Vector2 size = new Vector2(3,5);
	Vector2 realS;
	
	void Start () 
	{
		gameObject.AddComponent("MeshRenderer");
		gameObject.AddComponent("MeshFilter");
		gameObject.AddComponent("CapsuleCollider");

				
		this.gameObject.transform.position = new Vector3 (transform.position.x * Menu.ns.x , 20 , 0);
		
		realS = new Vector2(size.x * Menu.ns.x , size.y * Menu.ns.y);
		
		Mesh mesh = Quad.Create(realS.x ,realS.y );
		gameObject.GetComponent<MeshFilter>().mesh = mesh;
		gameObject.GetComponent<MeshRenderer>().material = (Material) Resources.Load("Outros/Transparent");
		gameObject.GetComponent<MeshRenderer>().material.mainTexture = (Texture) Resources.Load("Texturas/C_C");
		gameObject.GetComponent<CapsuleCollider>().radius = realS.x * 0.9f;
		gameObject.GetComponent<CapsuleCollider>().height = realS.y * 2;
		gameObject.GetComponent<CapsuleCollider>().direction = 1;

		f_basejump = 0;
	}
	
	void Update () 
	{
		#region gravidade, velocidade e pulo
		f_jump = f_basejump * Time.deltaTime * 2;
		f_basejump -= f_jump;
		Debug.Log(f_coins);
				
		#endregion

		#region sequencia de ifs para resolver fisicas e etc			

	 	if(Input.touchCount == 1 || Input.GetKey(KeyCode.Space) )
		{
			if(jump)
			{
				f_basejump = -Physics.gravity.y * 0.55f;
				jump = false;
			}
			
			else if(f_coins > 1)
			{
				this.gameObject.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
			}
			
			
		}
		else
		{
			this.gameObject.rigidbody.constraints = (RigidbodyConstraints.FreezeRotation);
		}
		#endregion
		
	/*	if(this.f_jump < this.f_gravity / 3)
		{
			this.f_jump += this.f_gravity / 2;
		}*/
		
		if(_BgSpecial.stop == true || _Victory1.stop == true || _Victory2.stop == true)
		{
			transform.Translate(-_Speed.speed , 0 , 0);
		}
		
		if(transform.position.x < -2)
		{
			Application.LoadLevel ("SelecaoCenas");
		}
		
		transform.Translate(0, f_jump , 0);
	}
	
	
	void OnCollisionEnter(Collision c)
	{
		if(c.gameObject.CompareTag("ground"))
		{
			jump = true;
		}
	}
	
	void OnGUI()
	{
		GUI.TextArea(new Rect(50 , 50 , 100 , 20) , "Moedas: " + (int)f_coins);
	}
}
